/**
 * 加减控件
 */
var CustomAddBox = ccui.Layout.extend({
    contentText: null,
    defaultIndex: 0,
    parmas: [],
    addButton: null,
    subtractButton: null,
    parentNode: null,
    valueType: null,
    valueName: null,

    onEnterTransitionDidFinish: function () {
        ccui.Layout.prototype.onEnterTransitionDidFinish.call(this);
        var addButton = this.getNode(this.parentNode, this.addButton);
        var subtractButton = this.getNode(this.parentNode, this.subtractButton);
        TouchUtil.setOnclickListener(addButton, this.add.bind(this));
        TouchUtil.setOnclickListener(subtractButton, this.subtract.bind(this));

        this.changeText();
    },
    /**
     * 改变文本
     */
    changeText: function () {
        this.getNode(this.parentNode, this.contentText).setString(this.parmas[this.defaultIndex].label);
    },
    /**
     * 加
     */
    add: function () {
        if (this.parmas == null)
            return;
        if (this.defaultIndex < this.parmas.length - 1)
            this.defaultIndex++;
        this.changeText();
    },
    /**
     * 减
     */
    subtract: function () {
        if (this.parmas == null)
            return;
        if (this.defaultIndex > 0)
            this.defaultIndex--;
        this.changeText();
    },
    /**
     * 取得node
     * @param node
     * @param str
     */
    getNode: function (node, str) {
        var names = str.split(".");
        for (var i = 0; i < names.length; i++) {
            node = node[names[i]];
        }
        return node;
    }
});